package cn.hl.product.controller;

import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.PageList;
import cn.hl.product.domain.Product;
import cn.hl.product.query.ProductQuery;
import cn.hl.product.service.IProductService;
import cn.hl.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    public IProductService productService;

    /**
     * 接口：添加或修改
     * @param product  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "保存服务",sn = "product:save")
    public AjaxResult addAndUpdate(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.add(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除服务",sn = "product:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @PreAuthorize(name = "服务批量删除",sn = "product:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                productService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取服务",sn = "product:get")
    public Product findById(@PathVariable("id")Long id){
            return productService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @PreAuthorize(name = "获取所有服务",sn = "product:getAll")
    public List<Product> findAll(){
        return productService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "服务列表",sn = "product:list")
    public PageList<Product> queryPage(@RequestBody ProductQuery query) {
        return productService.queryPage(query);
    }

    /**
     * 接口：上架
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids, HttpServletRequest request) {
        try {
            return productService.onsale(ids,request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上架失败！" + e.getMessage());
        }
    }

    /**
     * 接口：下架
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids, HttpServletRequest request) {
        try {
            return productService.offsale(ids,request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下架失败！" + e.getMessage());
        }
    }
}
